EMMA Coverage Report (generated Wed Apr 13 11:47:01 CEST 2016)
[all classes][caldwell.ben.trolly]

COVERAGE SUMMARY FOR SOURCE FILE [Trolly.java]

nameclass, %method, %block, %line, %
Trolly.java100% (13/13)87%  (41/47)82%  (1151/1401)80%  (230,9/289)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class Trolly$TrollyAdapter100% (1/1)50%  (2/4)47%  (60/128)54%  (15/28)
convertToString (Cursor): CharSequence 0%   (0/1)0%   (0/6)0%   (0/1)
runQueryOnBackgroundThread (CharSequence): Cursor 0%   (0/1)0%   (0/62)0%   (0/12)
Trolly$TrollyAdapter (Context, int, Cursor, String [], int []): void 100% (1/1)100% (12/12)100% (3/3)
bindView (View, Context, Cursor): void 100% (1/1)100% (48/48)100% (12/12)
     
class Trolly100% (1/1)94%  (17/18)81%  (741/914)79%  (156/197)
addExtraItems (): void 0%   (0/1)0%   (0/124)0%   (0/24)
onListItemClick (ListView, View, int, long): void 100% (1/1)59%  (45/76)59%  (10/17)
onOptionsItemSelected (MenuItem): boolean 100% (1/1)89%  (34/38)92%  (11/12)
onContextItemSelected (MenuItem): boolean 100% (1/1)94%  (119/126)86%  (25/29)
onCreateContextMenu (ContextMenu, View, ContextMenu$ContextMenuInfo): void 100% (1/1)97%  (88/91)86%  (18/21)
onCreate (Bundle): void 100% (1/1)97%  (59/61)94%  (15/16)
onCreateDialog (int): Dialog 100% (1/1)99%  (149/151)97%  (34/35)
<static initializer> 100% (1/1)100% (18/18)100% (2/2)
Trolly (): void 100% (1/1)100% (3/3)100% (2/2)
access$000 (): String [] 100% (1/1)100% (2/2)100% (1/1)
access$100 (Trolly): AutoCompleteTextView 100% (1/1)100% (3/3)100% (1/1)
access$200 (Trolly): EditText 100% (1/1)100% (3/3)100% (1/1)
access$300 (Trolly): Uri 100% (1/1)100% (3/3)100% (1/1)
checkout (): void 100% (1/1)100% (95/95)100% (14/14)
onCreateOptionsMenu (Menu): boolean 100% (1/1)100% (42/42)100% (10/10)
onPause (): void 100% (1/1)100% (10/10)100% (4/4)
onResume (): void 100% (1/1)100% (27/27)100% (7/7)
updateList (): void 100% (1/1)100% (41/41)100% (4/4)
     
class Trolly$3100% (1/1)50%  (1/2)86%  (6/7)50%  (1/2)
onClick (DialogInterface, int): void 0%   (0/1)0%   (0/1)0%   (0/1)
Trolly$3 (Trolly): void 100% (1/1)100% (6/6)100% (1/1)
     
class Trolly$7100% (1/1)50%  (1/2)86%  (6/7)50%  (1/2)
onClick (DialogInterface, int): void 0%   (0/1)0%   (0/1)0%   (0/1)
Trolly$7 (Trolly): void 100% (1/1)100% (6/6)100% (1/1)
     
class Trolly$9100% (1/1)50%  (1/2)86%  (6/7)50%  (1/2)
onClick (DialogInterface, int): void 0%   (0/1)0%   (0/1)0%   (0/1)
Trolly$9 (Trolly): void 100% (1/1)100% (6/6)100% (1/1)
     
class Trolly$AutoFillAdapter100% (1/1)100% (5/5)95%  (100/105)95%  (21/22)
runQueryOnBackgroundThread (CharSequence): Cursor 100% (1/1)92%  (57/62)92%  (11/12)
Trolly$AutoFillAdapter (Context, Cursor): void 100% (1/1)100% (9/9)100% (3/3)
bindView (View, Context, Cursor): void 100% (1/1)100% (9/9)100% (2/2)
convertToString (Cursor): CharSequence 100% (1/1)100% (6/6)100% (1/1)
newView (Context, Cursor, ViewGroup): View 100% (1/1)100% (19/19)100% (4/4)
     
class Trolly$2100% (1/1)100% (2/2)99%  (118/119)99%  (22,9/23)
onClick (View): void 100% (1/1)99%  (112/113)99%  (21,9/22)
Trolly$2 (Trolly): void 100% (1/1)100% (6/6)100% (1/1)
     
class Trolly$1100% (1/1)100% (2/2)100% (14/14)100% (5/5)
Trolly$1 (Trolly): void 100% (1/1)100% (6/6)100% (1/1)
onClick (View): void 100% (1/1)100% (8/8)100% (4/4)
     
class Trolly$10100% (1/1)100% (2/2)100% (18/18)100% (3/3)
Trolly$10 (Trolly): void 100% (1/1)100% (6/6)100% (1/1)
onClick (DialogInterface, int): void 100% (1/1)100% (12/12)100% (2/2)
     
class Trolly$4100% (1/1)100% (2/2)100% (30/30)100% (5/5)
Trolly$4 (Trolly): void 100% (1/1)100% (6/6)100% (1/1)
onClick (DialogInterface, int): void 100% (1/1)100% (24/24)100% (4/4)
     
class Trolly$5100% (1/1)100% (2/2)100% (7/7)100% (2/2)
Trolly$5 (Trolly): void 100% (1/1)100% (6/6)100% (1/1)
onClick (DialogInterface, int): void 100% (1/1)100% (1/1)100% (1/1)
     
class Trolly$6100% (1/1)100% (2/2)100% (17/17)100% (3/3)
Trolly$6 (Trolly): void 100% (1/1)100% (6/6)100% (1/1)
onClick (DialogInterface, int): void 100% (1/1)100% (11/11)100% (2/2)
     
class Trolly$8100% (1/1)100% (2/2)100% (28/28)100% (5/5)
Trolly$8 (Trolly): void 100% (1/1)100% (6/6)100% (1/1)
onClick (DialogInterface, int): void 100% (1/1)100% (22/22)100% (4/4)

1/**
2        <Trolly is a simple shopping list application for android phones.>
3        Copyright (C) 2009  Ben Caldwell
4 
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation, either version 3 of the License, or
8    (at your option) any later version.
9 
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 
20package caldwell.ben.trolly;
21 
22import java.util.ArrayList;
23 
24import caldwell.ben.provider.Trolly.ShoppingList;
25import android.app.AlertDialog;
26import android.app.Dialog;
27import android.app.ListActivity;
28import android.content.ContentResolver;
29import android.content.ContentUris;
30import android.content.ContentValues;
31import android.content.Context;
32import android.content.DialogInterface;
33import android.content.Intent;
34import android.content.SharedPreferences;
35import android.database.Cursor;
36import android.graphics.Color;
37import android.graphics.Paint;
38import android.net.Uri;
39import android.os.Bundle;
40import android.preference.PreferenceManager;
41import android.view.ContextMenu;
42import android.view.LayoutInflater;
43import android.view.Menu;
44import android.view.MenuItem;
45import android.view.View;
46import android.view.ViewGroup;
47import android.view.ContextMenu.ContextMenuInfo;
48import android.widget.AdapterView;
49import android.widget.AutoCompleteTextView;
50import android.widget.Button;
51import android.widget.CursorAdapter;
52import android.widget.EditText;
53import android.widget.Filterable;
54import android.widget.ListView;
55import android.widget.SimpleCursorAdapter;
56import android.widget.TextView;
57 
58public class Trolly extends ListActivity {
59 
60        //        private static final String TAG = "Trolly";
61 
62        public static final String KEY_ITEM = "items";
63        public static boolean adding = false;
64 
65        /**
66         * TrollyAdapter allows crossing items off the list and filtering
67         * on user text input.
68         * @author Ben
69         *
70         */
71        private static class TrollyAdapter extends SimpleCursorAdapter implements Filterable {
72 
73                private ContentResolver mContent;   
74 
75                public TrollyAdapter(Context context, int layout, Cursor c,
76                                String[] from, int[] to) {
77                        super(context, layout, c, from, to);
78                        mContent = context.getContentResolver();
79                }
80 
81                @Override
82                public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
83                        if (getFilterQueryProvider() != null) {
84                                return getFilterQueryProvider().runQuery(constraint);
85                        }
86 
87                        StringBuilder buffer = null;
88                        String[] args = null;
89                        if (constraint != null) {
90                                buffer = new StringBuilder();
91                                buffer.append("UPPER(");
92                                buffer.append(ShoppingList.ITEM);
93                                buffer.append(") GLOB ?");
94                                args = new String[] { "*" + constraint.toString().toUpperCase() + "*" };
95                        }
96 
97                        return mContent.query(ShoppingList.CONTENT_URI, PROJECTION,
98                                        buffer == null ? null : buffer.toString(), args,
99                                                        null);
100                }
101 
102                @Override
103                public void bindView(View view, Context context, Cursor cursor) {
104                        TextView item = (TextView)view.findViewById(R.id.item);
105                        item.setText(cursor.getString(cursor.getColumnIndex(ShoppingList.ITEM)));
106                        switch(cursor.getInt(cursor.getColumnIndex(ShoppingList.STATUS))){
107                        case ShoppingList.OFF_LIST:
108                                item.setPaintFlags(item.getPaintFlags() & ~Paint.STRIKE_THRU_TEXT_FLAG);
109                                item.setTextColor(Color.DKGRAY);
110                                break;
111                        case ShoppingList.ON_LIST:
112                                item.setPaintFlags(item.getPaintFlags() & ~Paint.STRIKE_THRU_TEXT_FLAG);
113                                item.setTextColor(Color.GREEN);
114                                break;
115                        case ShoppingList.IN_TROLLEY:
116                                item.setPaintFlags(item.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
117                                item.setTextColor(Color.GRAY);
118                                break;
119                        }
120                }
121 
122                @Override
123                public CharSequence convertToString(Cursor cursor) {
124                        return cursor.getString(cursor.getColumnIndex(ShoppingList.ITEM));
125                }
126 
127        }
128 
129        /**
130         * AutoFillAdapter is an adapter for the AutoCompleteTextView at the top of the Trolly Activity
131         * @author Ben Caldwel
132         *
133         */
134        private static class AutoFillAdapter extends CursorAdapter implements Filterable {
135 
136                private ContentResolver mContent;
137 
138                public AutoFillAdapter(Context context, Cursor c) {
139                        super(context, c);
140                        mContent = context.getContentResolver();
141                }
142 
143                @Override
144                public void bindView(View view, Context context, Cursor cursor) {
145                        ((TextView) view).setText(cursor.getString(cursor.getColumnIndex(ShoppingList.ITEM)));
146                }
147 
148                @Override
149                public View newView(Context context, Cursor cursor, ViewGroup parent) {
150                        final LayoutInflater inflater = LayoutInflater.from(context);
151                        final TextView view = (TextView)inflater.inflate(
152                                        android.R.layout.simple_dropdown_item_1line, 
153                                        parent,false);
154                        view.setText(cursor.getString(cursor.getColumnIndex(ShoppingList.ITEM)));
155                        return view;
156                }
157 
158                @Override
159                public CharSequence convertToString(Cursor cursor) {
160                        return cursor.getString(cursor.getColumnIndex(ShoppingList.ITEM)); 
161                }
162 
163                @Override
164                public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
165                        if (getFilterQueryProvider() != null) {
166                                return getFilterQueryProvider().runQuery(constraint);
167                        }
168 
169                        StringBuilder buffer = null;
170                        String[] args = null;
171                        if (constraint != null) {
172                                buffer = new StringBuilder();
173                                buffer.append("UPPER(");
174                                buffer.append(ShoppingList.ITEM);
175                                buffer.append(") GLOB ?");
176                                args = new String[] { "*" + constraint.toString().toUpperCase() + "*" };
177                        }
178 
179                        return mContent.query(ShoppingList.CONTENT_URI, PROJECTION,
180                                        buffer == null ? null : buffer.toString(), args,
181                                                        null);
182                }
183        }
184        /**
185         * The columns we are interested in from the database
186         */
187        private static final String[] PROJECTION = new String[] {
188                ShoppingList._ID, // 0
189                ShoppingList.ITEM, // 1
190                ShoppingList.STATUS, // 2
191        };
192 
193        // Menu item ids
194        public static final int MENU_ITEM_DELETE = Menu.FIRST;
195        public static final int MENU_ITEM_INSERT = Menu.FIRST + 1;
196        public static final int MENU_ITEM_CHECKOUT = Menu.FIRST + 2;
197        public static final int MENU_ITEM_PREFERENCE = Menu.FIRST + 3;
198        public static final int MENU_ITEM_ON_LIST = Menu.FIRST + 4;
199        public static final int MENU_ITEM_OFF_LIST = Menu.FIRST + 5;
200        public static final int MENU_ITEM_IN_TROLLEY = Menu.FIRST + 6;
201        public static final int MENU_ITEM_EDIT = Menu.FIRST + 7;
202        public static final int MENU_ITEM_CLEAR = Menu.FIRST + 8;
203        public static final int MENU_ITEM_RESET = Menu.FIRST + 9;
204 
205        /**
206         * Case selections for the type of dialog box displayed
207         */
208        private static final int DIALOG_DELETE = 1;
209        private static final int DIALOG_EDIT = 2;
210        private static final int DIALOG_CLEAR = 3;
211        private static final int DIALOG_RESET = 4;
212 
213        //Use private members for dialog textview to prevent weird persistence problem
214        private EditText mDialogEdit;
215        private TextView mDialogText;
216        private View mDialogView;
217 
218        private Cursor mCursor;
219        private AutoCompleteTextView mTextBox;
220        private Button btnAdd;
221        private TrollyAdapter mAdapter;
222        private SharedPreferences mPrefs;
223 
224        private Uri mUri;
225 
226 
227        /** Called when the activity is first created. */
228        @Override
229        public void onCreate(Bundle savedInstanceState) {
230                super.onCreate(savedInstanceState);
231 
232                // If no data was given in the intent (because we were started
233                // as a MAIN activity), then use our default content provider.
234                Intent intent = getIntent();
235                if (intent.getData() == null) {
236                        intent.setData(ShoppingList.CONTENT_URI);
237                }
238 
239                setContentView(R.layout.trolly);  
240                // Inform the list we provide context menus for items
241                getListView().setOnCreateContextMenuListener(this);
242 
243                adding = false;
244                updateList();
245 
246                mTextBox = (AutoCompleteTextView)findViewById(R.id.textbox);
247                btnAdd = (Button)findViewById(R.id.btn_add);
248 
249                mTextBox.setOnClickListener(new Button.OnClickListener(){
250                        @Override
251                        public void onClick(View view) {
252                                //If the text box is clicked while full-list adding, stop adding
253                                if (adding) {
254                                        adding = false;
255                                        updateList();
256                                }
257                        }
258                });
259 
260                btnAdd.setOnClickListener(new Button.OnClickListener(){
261                        @Override
262                        public void onClick(View view) {
263                                //If there is a string in the textbox then add it to the list
264                                if (mTextBox.getText().length()>0) {
265                                        Cursor c = getContentResolver().query(getIntent().getData(), 
266                                                        PROJECTION, 
267                                                        ShoppingList.ITEM+"='"+mTextBox.getText()+"'", 
268                                                        null, 
269                                                        null);
270                                        c.moveToFirst();
271                                        if (c == null 
272                                                        || c.isBeforeFirst() 
273                                                        || c.getInt(c.getColumnIndex(ShoppingList.STATUS))==ShoppingList.ON_LIST) {
274                                                ContentValues values = new ContentValues();
275                                                values.put(ShoppingList.ITEM, mTextBox.getText().toString());
276                                                getContentResolver().insert(ShoppingList.CONTENT_URI,values);
277                                        } else {
278                                                ContentValues values = new ContentValues();
279                                                values.put(ShoppingList.STATUS, ShoppingList.ON_LIST);
280                                                long id = c.getLong(c.getColumnIndex(ShoppingList._ID));
281                                                Uri uri = ContentUris.withAppendedId(getIntent().getData(), id);
282                                                getContentResolver().update(uri, values, null, null);
283                                        }
284                                        mTextBox.setText("");
285                                } else {
286                                        adding = !adding;
287                                        updateList();
288                                }
289                        }
290                });
291                //mPrefs = getSharedPreferences(null, MODE_PRIVATE);
292                mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
293 
294                if (intent.hasExtra(org.openintents.intents.ShoppingListIntents.EXTRA_STRING_ARRAYLIST_SHOPPING))
295                        addExtraItems();
296        }
297 
298        protected void updateList() {
299                //set up the list cursor
300                mCursor = managedQuery(getIntent().getData(), 
301                                PROJECTION, 
302                                adding ? null: ShoppingList.STATUS+"<>"+ShoppingList.OFF_LIST,
303                                                null,
304                                                ShoppingList.DEFAULT_SORT_ORDER);
305 
306                //set the list adapter
307                mAdapter = new TrollyAdapter(this, R.layout.shoppinglist_item, mCursor,
308                                new String[] { ShoppingList.ITEM}, new int[] { R.id.item});
309                setListAdapter(mAdapter);
310        }
311 
312        @Override
313        protected void onResume() {
314                super.onResume();
315 
316                adding = false;
317                updateList();
318 
319                Cursor cAutoFill = managedQuery(getIntent().getData(), 
320                                PROJECTION, 
321                                null, 
322                                null,
323                                ShoppingList.DEFAULT_SORT_ORDER);
324 
325                AutoFillAdapter autoFillAdapter = new AutoFillAdapter(this, cAutoFill);
326                mTextBox.setAdapter(autoFillAdapter);
327        }
328 
329        @Override
330        protected void onPause() {
331                super.onPause();
332                SharedPreferences.Editor ed = mPrefs.edit();
333                ed.commit();
334        }
335 
336        @Override
337        protected void onListItemClick(ListView l, View v, int position, long id) {
338                //super.onListItemClick(l, v, position, id);
339                Uri uri = ContentUris.withAppendedId(getIntent().getData(), id);
340                Cursor c = getContentResolver().query(uri, PROJECTION, null, null, null);
341                c.moveToFirst();
342                ContentValues values = new ContentValues();
343                switch (c.getInt(c.getColumnIndex(ShoppingList.STATUS)))
344                {
345                case ShoppingList.OFF_LIST:
346                        //move from off the list to on the list
347                        values.put(ShoppingList.STATUS, ShoppingList.ON_LIST);
348                        getContentResolver().update(uri, values, null, null);
349                        break;
350                case ShoppingList.ON_LIST:
351                        values.put(ShoppingList.STATUS, ShoppingList.IN_TROLLEY);
352                        getContentResolver().update(uri, values, null, null);
353                        break;
354                case ShoppingList.IN_TROLLEY:
355                        //move back from in the trolley to on the list
356                        values.put(ShoppingList.STATUS, ShoppingList.ON_LIST);
357                        getContentResolver().update(uri, values, null, null);
358                        break;
359                }
360                if (adding) {
361                        adding = false;                                
362                        updateList();
363                }                
364        }
365 
366        @Override
367        public boolean onContextItemSelected(MenuItem item) {
368                AdapterView.AdapterContextMenuInfo info;
369                try {
370                        info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
371                } catch (ClassCastException e) {
372                        return false;
373                }
374 
375                Cursor cursor = (Cursor) getListAdapter().getItem(info.position);
376                if (cursor == null) {
377                        // For some reason the requested item isn't available, do nothing
378                        return false;
379                }
380 
381                mUri = ContentUris.withAppendedId(getIntent().getData(), 
382                                cursor.getLong(cursor.getColumnIndex(ShoppingList._ID)));
383                Cursor c = getContentResolver().query(mUri, PROJECTION, null, null, null);
384                c.moveToFirst();
385                ContentValues values = new ContentValues();
386 
387                switch (item.getItemId()) {
388                case MENU_ITEM_ON_LIST:
389                        // Change to "on list" status
390                        values.put(ShoppingList.STATUS, ShoppingList.ON_LIST);
391                        getContentResolver().update(mUri, values, null, null);
392                        return true;                        
393                case MENU_ITEM_OFF_LIST:
394                        // Change to "off list" status
395                        values.put(ShoppingList.STATUS, ShoppingList.OFF_LIST);
396                        getContentResolver().update(mUri, values, null, null);
397                        return true;
398                case MENU_ITEM_IN_TROLLEY:
399                        //Change to "in trolley" status
400                        values.put(ShoppingList.STATUS, ShoppingList.IN_TROLLEY);
401                        getContentResolver().update(mUri, values, null, null);
402                        return true;
403                case MENU_ITEM_EDIT:
404                        //Show edit dialog
405                        showDialog(DIALOG_EDIT);
406                        mDialogEdit.setText(c.getString(c.getColumnIndex(ShoppingList.ITEM)));
407                        return true;
408                case MENU_ITEM_DELETE:
409                        //Show are you sure dialog then delete
410                        showDialog(DIALOG_DELETE);
411                        mDialogText.setText(c.getString(c.getColumnIndex(ShoppingList.ITEM)));
412                        return true;
413                }
414                return false;
415        }
416 
417        @Override
418        public void onCreateContextMenu(ContextMenu menu, View v,
419                        ContextMenuInfo menuInfo) {
420                AdapterView.AdapterContextMenuInfo info;
421                try {
422                        info = (AdapterView.AdapterContextMenuInfo) menuInfo;
423                } catch (ClassCastException e) {
424                        return;
425                }
426                Cursor cursor = (Cursor)getListAdapter().getItem(info.position);
427                if (cursor == null) {
428                        // For some reason the requested item isn't available, do nothing
429                        return;
430                }
431                // Setup the menu header
432                menu.setHeaderTitle(cursor.getString(cursor.getColumnIndex(ShoppingList.ITEM)));
433                int status = cursor.getInt(cursor.getColumnIndex(ShoppingList.STATUS));
434 
435                //Add context menu items depending on current state
436                switch (status) {
437                case ShoppingList.OFF_LIST:
438                        menu.add(0, MENU_ITEM_ON_LIST, 0, R.string.move_on_list);
439                        menu.add(0, MENU_ITEM_IN_TROLLEY, 0, R.string.move_in_trolley);
440                        break;
441                case ShoppingList.ON_LIST:
442                        menu.add(0, MENU_ITEM_IN_TROLLEY, 0, R.string.move_in_trolley);
443                        menu.add(0, MENU_ITEM_OFF_LIST, 0, R.string.move_off_list);
444                        break;
445                case ShoppingList.IN_TROLLEY:
446                        menu.add(0, MENU_ITEM_ON_LIST, 0, R.string.move_on_list);
447                        menu.add(0, MENU_ITEM_OFF_LIST, 0, R.string.move_off_list);
448                        break;
449                }
450 
451                // Add context menu items that are relevant for all items
452                menu.add(0, MENU_ITEM_EDIT, 0, R.string.edit_item);
453                menu.add(0, MENU_ITEM_DELETE, 0, R.string.delete_item);
454        }
455 
456        @Override
457        public boolean onCreateOptionsMenu(Menu menu) {
458                super.onCreateOptionsMenu(menu);
459                menu.add(0, MENU_ITEM_CHECKOUT, 2, R.string.checkout)
460                .setIcon(android.R.drawable.ic_media_next);
461                menu.add(0, MENU_ITEM_CLEAR, 3, R.string.clear_list)
462                .setIcon(android.R.drawable.ic_menu_revert);
463                menu.add (0, MENU_ITEM_PREFERENCE, 4, R.string.preferences)
464                .setIcon(android.R.drawable.ic_menu_preferences);
465                menu.add(0, MENU_ITEM_RESET, 5, R.string.reset_list)
466                .setIcon(android.R.drawable.ic_menu_delete);
467                return true;
468        }
469 
470        @Override
471        public boolean onOptionsItemSelected(MenuItem item) {
472                switch (item.getItemId()) {
473                case MENU_ITEM_CHECKOUT:
474                        //Change all items from in trolley to off list
475                        checkout();
476                        return true;
477                case MENU_ITEM_CLEAR:
478                        //Change all items to off list
479                        showDialog(DIALOG_CLEAR);
480                        mDialogText.setText(R.string.clear_prompt);
481                        return true;
482                case MENU_ITEM_RESET:
483                        //Change all items to off list
484                        showDialog(DIALOG_RESET);
485                        mDialogText.setText(R.string.reset_prompt);
486                        return true;
487                case MENU_ITEM_PREFERENCE:
488                        startActivity(new Intent(this,TrollyPreferences.class));
489                        return true;
490                }
491                return super.onOptionsItemSelected(item);
492        }
493 
494        @Override
495        protected Dialog onCreateDialog(int id) {
496                LayoutInflater factory = LayoutInflater.from(this);
497                switch (id) {
498                case DIALOG_EDIT:
499                        mDialogView = factory.inflate(R.layout.dialog_edit, null);
500                        mDialogEdit = (EditText)mDialogView.findViewById(R.id.edit);
501                        return new AlertDialog.Builder(this)
502                        .setTitle(R.string.edit_item)
503                        .setView(mDialogView)
504                        .setPositiveButton(R.string.dialog_ok, new DialogInterface.OnClickListener() {
505                                public void onClick(DialogInterface dialog, int whichButton) {
506                                        /* User clicked OK so do some stuff */
507                                        ContentValues values = new ContentValues();
508                                        values.put(ShoppingList.ITEM, mDialogEdit.getText().toString());
509                                        getContentResolver().update(mUri, values, null, null);
510                                }
511                        })
512                        .setNegativeButton(R.string.dialog_cancel, new DialogInterface.OnClickListener() {
513                                public void onClick(DialogInterface dialog, int whichButton) {
514                                        /* User clicked cancel so do some stuff */
515                                }
516                        })
517                        .create();
518                case DIALOG_DELETE:
519                        mDialogView = factory.inflate(R.layout.dialog_confirm, null);
520                        mDialogText = (TextView)mDialogView.findViewById(R.id.dialog_confirm_prompt);
521                        return new AlertDialog.Builder(this)
522                        .setTitle(R.string.delete_item)
523                        .setView(mDialogView)
524                        .setPositiveButton(R.string.dialog_ok, new DialogInterface.OnClickListener() {
525                                public void onClick(DialogInterface dialog, int whichButton) {
526                                        /* User clicked OK so do some stuff */
527                                        getContentResolver().delete(mUri, null, null);
528                                }
529                        })
530                        .setNegativeButton(R.string.dialog_cancel, new DialogInterface.OnClickListener() {
531                                public void onClick(DialogInterface dialog, int whichButton) {
532                                        /* User clicked cancel so do some stuff */
533                                }
534                        })
535                        .create();
536                case DIALOG_CLEAR:
537                        mDialogView = factory.inflate(R.layout.dialog_confirm, null);
538                        mDialogText = (TextView)mDialogView.findViewById(R.id.dialog_confirm_prompt);
539                        return new AlertDialog.Builder(this)
540                        .setTitle(R.string.clear_list)
541                        .setView(mDialogView)
542                        .setPositiveButton(R.string.dialog_ok, new DialogInterface.OnClickListener() {
543                                public void onClick(DialogInterface dialog, int whichButton) {
544                                        ContentValues values = new ContentValues();
545                                        //Set all items status to "off list"
546                                        values.put(ShoppingList.STATUS, ShoppingList.OFF_LIST);
547                                        getContentResolver().update(getIntent().getData(), values, null, null);
548                                }
549                        })
550                        .setNegativeButton(R.string.dialog_cancel, new DialogInterface.OnClickListener() {
551                                public void onClick(DialogInterface dialog, int whichButton) {
552                                        /* User clicked cancel so do some stuff */
553                                }
554                        })
555                        .create();
556                case DIALOG_RESET:
557                        mDialogView = factory.inflate(R.layout.dialog_confirm, null);
558                        mDialogText = (TextView)mDialogView.findViewById(R.id.dialog_confirm_prompt);
559                        return new AlertDialog.Builder(this)
560                        .setTitle(R.string.reset_list)
561                        .setView(mDialogView)
562                        .setPositiveButton(R.string.dialog_ok, new DialogInterface.OnClickListener() {
563                                public void onClick(DialogInterface dialog, int whichButton) {
564                                        //Permanently delete all items from the list
565                                        getContentResolver().delete(getIntent().getData(), null, null);
566                                }
567                        })
568                        .setNegativeButton(R.string.dialog_cancel, new DialogInterface.OnClickListener() {
569                                public void onClick(DialogInterface dialog, int whichButton) {
570                                        /* User clicked cancel so do some stuff */
571                                }
572                        })
573                        .create();
574                }
575                return null;
576        }
577 
578        /**
579         * Change all items marked as "in trolley" to "off list"
580         */
581        private void checkout() {
582                Cursor c = managedQuery(getIntent().getData(), PROJECTION, null, null,
583                                ShoppingList.DEFAULT_SORT_ORDER);
584                c.moveToFirst();
585                ContentValues values = new ContentValues();
586                values.put(ShoppingList.STATUS, ShoppingList.OFF_LIST);
587                Uri uri;
588                int status;
589                long id;
590                //loop through all items in the list
591                while (!c.isAfterLast()) {
592                        status = c.getInt(c.getColumnIndex(ShoppingList.STATUS));
593                        //if the item is not in the trolley jump to the next one
594                        if (status == ShoppingList.IN_TROLLEY) {
595                                id = c.getLong(c.getColumnIndexOrThrow(ShoppingList._ID));
596                                uri = ContentUris.withAppendedId(getIntent().getData(), id);
597                                //Update the status of this item (in trolley) to "off list"
598                                getContentResolver().update(uri, values, null, null);
599                                //Cleanup the list by deleting double up items that have been checked out
600                                getContentResolver().delete(getIntent().getData(),
601                                                ShoppingList.ITEM + "='" 
602                                                                + c.getString(c.getColumnIndex(ShoppingList.ITEM)) 
603                                                                + "' AND " + ShoppingList._ID + "<>" + id
604                                                                + " AND " + ShoppingList.STATUS + "=" + ShoppingList.OFF_LIST, 
605                                                                null);
606                        }
607                        c.moveToNext();
608                }
609        }
610 
611        /**
612         * Add items received as extras in the intent to the list
613         */
614        private void addExtraItems() {
615                ArrayList<String> list = getIntent().getStringArrayListExtra(org.openintents.intents.ShoppingListIntents.EXTRA_STRING_ARRAYLIST_SHOPPING);
616                Cursor c;
617                long id;
618                Uri uri;
619                for (String item : list) {
620                        c = getContentResolver().query(getIntent().getData(), 
621                                        PROJECTION, 
622                                        ShoppingList.ITEM + "='" + item + "'", 
623                                        null, 
624                                        null);
625 
626                        //If there is no match then just add the item to the list with "on list" status
627                        c.moveToFirst();
628                        if (c.isBeforeFirst()) {
629                                ContentValues values = new ContentValues();
630                                values.put(ShoppingList.ITEM, item);
631                                values.put(ShoppingList.STATUS, ShoppingList.ON_LIST);
632                                getContentResolver().insert(getIntent().getData(), values);
633                        } else {
634                                //If there is a list item that matches this item...
635                                //get status of existing item
636                                int status = c.getInt(c.getColumnIndex(ShoppingList.STATUS));
637                                if (status == ShoppingList.OFF_LIST) {
638                                        //move an existing "off list" item to "on list"
639                                        ContentValues values = new ContentValues();
640                                        values.put(ShoppingList.STATUS, ShoppingList.ON_LIST);
641                                        id = c.getLong(c.getColumnIndex(ShoppingList._ID));
642                                        uri = ContentUris.withAppendedId(getIntent().getData(), id);
643                                        getContentResolver().update(uri, values, null, null);                                    
644                                } else { 
645                                        /**If an existing item already has a status of "on list" 
646                                         * then create a new (duplicate) item with "on list" status.
647                                         * This allows for the case where an item is already on the list
648                                         * but is added again from another source.
649                                         */
650                                        ContentValues values = new ContentValues();
651                                        values.put(ShoppingList.ITEM, item);
652                                        values.put(ShoppingList.STATUS, ShoppingList.ON_LIST);
653                                        getContentResolver().insert(getIntent().getData(), values);
654                                }
655                        }                    
656                }
657        }
658}
659 
660 
661 
662 

[all classes][caldwell.ben.trolly]
EMMA 2.0.5312 (C) Vladimir Roubtsov